package com.itheima.homework4.comparator;

import java.util.TreeSet;

public class Test2 {
    public static void main(String[] args) {
        //lambda表达式实现Comparator接口排序
        TreeSet<Student2> ts2 = new TreeSet<>((o1, o2) -> {
            //按照成绩从小到大排序
            int r = o2.getScore() - o1.getScore();
            //按照年龄从小到大排序
            r = r == 0 ? o1.getAge() - o2.getAge() : r;
            //姓名按照字典顺序排
            r = r == 0 ? o1.getName().compareTo(o2.getName()) : r;
            return r;
        });
        //Student2有参构造，创建对象
        Student2 s1 = new Student2("Tom",20,90);
        Student2 s2 = new Student2("Jerry",22,95);
        Student2 s3 = new Student2("John",20,100);
        Student2 s4 = new Student2("Lily",22,100);
        Student2 s5 = new Student2("Lucy",22,90);
        Student2 s6 = new Student2("Kevin",22,90);
        //添加元素
        ts2.add(s1);
        ts2.add(s2);
        ts2.add(s3);
        ts2.add(s4);
        ts2.add(s5);
        ts2.add(s6);
        //foreach语句遍历
        for (Student2 student2 : ts2) {
            System.out.println(student2);
        }
    }
}
